package com.bsoft.gol.editor.dao;

import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import com.bsoft.gol.editor.editor.EditorQuestionnaireTitleCollection;
import com.bsoft.gol.editor.editor.request.QueryCollectionListRequest;
import com.bsoft.gol.editor.editor.response.QueryCollectionListResponse;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: 问卷题目收藏
 * @author: chenJij@bsoft.com.cn
 * @date: 2021/10/11 16:46
 */
public abstract class EditorQuestionnaireTitleCollectionDAO extends HibernateSupportDelegateDAO<EditorQuestionnaireTitleCollection> {
    public EditorQuestionnaireTitleCollectionDAO() {
        this.setKeyField("collectionId");
    }

    /**
     * 查询列表
     * @return
     */
    public List<QueryCollectionListResponse> queryEditorQuestionnaireList(final QueryCollectionListRequest req) {
        HibernateStatelessResultAction<List<QueryCollectionListResponse>> action = new AbstractHibernateStatelessResultAction<List<QueryCollectionListResponse>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql =new StringBuffer();
                 sql.append("SELECT " +
                         " a.title_id AS titleId, " +
                         " a.title_name AS titleName, " +
                         " a.widget_type AS widgetType, " +
                         " a.upload_file_type AS uploadFileType, " +
                         " a.write_flag AS writeFlag, " +
                         " b.collection_id AS collectionId " +
                         "FROM " +
                         " editor_questionnaire_title a, " +
                         " editor_questionnaire_title_collection b " +
                         "WHERE " +
                         " a.title_id = b.title_id  and collector_id=:userId ");
                if(CommonUtils.isNotEmpty(req.getTitleName())) {
                    sql.append(" and  a.title_name like:titleName");
                }
                sql.append(" order by  b.create_time asc");

                SQLQuery sqlQuery = ss.createSQLQuery(sql.toString());
                sqlQuery.setParameter("userId", req.getUserId());

                if(CommonUtils.isNotEmpty(req.getTitleName())) {
                    sqlQuery.setParameter("titleName", req.getTitleName()+"%");

                }
                sqlQuery = (SQLQuery) sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

                sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

                List<Map> list = sqlQuery.list();
                List<QueryCollectionListResponse> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, QueryCollectionListResponse.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);

            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}
